﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command.MenuCommand;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.MenuCommandHandler
{
    /// <summary>
    /// 判断删除的菜单是否为父级命令执行
    /// </summary>
    public class DeleteMenuJudgeCommandHandler : IRequestHandler<DeleteMenuJudgeCommand, APIResult<List<MenuModel>>>
    {
        private readonly IBaseRepository<MenuModel> menuRepos;

        public DeleteMenuJudgeCommandHandler(IBaseRepository<MenuModel> menuRepos)
        {
            this.menuRepos = menuRepos;
        }

        /// <summary>
        /// 判断删除的菜单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<List<MenuModel>>> Handle(DeleteMenuJudgeCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<MenuModel>> result = new APIResult<List<MenuModel>>();
            var list = menuRepos.GetAll().Result.Where(x=>x.ParentId == request.MenuId).ToList();
            if (list.Count > 0)
            {
                result.Code = APIEnum.是父级;
                result.Msg = APIEnum.是父级.ToString();
                result.data = list;
            }else
            {
                result.Code = APIEnum.不是父级;
                result.Msg = APIEnum.不是父级.ToString();
            }

            return await Task.FromResult(result);

        }
    }
}
